package com.futao.controller;

import com.futao.entity.User;
import com.futao.service.UserService;
import com.futao.sys.model.MyException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author futao
 * Created on 18-6-5-上午10:48.
 */
@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户列表
     *
     * @return
     */
    @ApiOperation(value = "用户列表", notes = "用户列表")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<User> getUserList() throws InterruptedException {
        //为了触发服务降级逻辑，我们可以将服务提供者eureka-client的逻辑加一些延迟
//        Thread.sleep(5000L);
        List<User> list = userService.list();
        System.out.println(list);
        return list;
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postUser(@ModelAttribute User user) throws MyException {
//        users.put(user.getId(), user);
        int add = userService.add(user);
        return String.valueOf(add);
    }

    /**
     * 通过id获取用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(
            @PathVariable
                    String id
    ) throws MyException {
        return userService.get(id);
    }

    /**
     * 修改用户信息
     *
     * @param id
     * @param user
     * @return
     */
    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要修改的用户的id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "user实体", required = true, dataType = "User")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(
            @PathVariable
                    Long id,
            @ModelAttribute
                    User user
    ) {
//        User userOld = users.get(id);
//        userOld.setAge(user.getAge());
//        userOld.setName(user.getName());
//        users.put(userOld.getId(), userOld);
        return "update success";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUser(
            @PathVariable
                    Long id
    ) {
//        users.remove(id);
        return "delete success";
    }


    /**
     * 添加
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(
            @ModelAttribute
                    User user
    ) {

        return "6";
    }

}
